package com.rs.game.player;

import java.io.Serializable;

import com.rs.game.Entity;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.player.controlers.Controler;
import com.rs.game.player.controlers.ControlerHandler;

public final class ControlerManager implements Serializable {

	private static final long serialVersionUID = 2084691334731830796L;
	
	private transient Player player;
	private transient Controler controler;
	private transient boolean inited;
	private Object[] lastControlerArguments;
	private String lastControler;
	public ControlerManager() {
		
	}
	
	public void setPlayer(Player player) {
		this.player = player;
	}
	
	public void startControler(String key, Object... parameters) {
		if(controler != null)
			forceStop();
		controler = ControlerHandler.getControler(key);
		if(controler == null)
			return;
		controler.setPlayer(player);
		lastControlerArguments = parameters;
		lastControler = key;
		controler.start();
		inited = true;
			
	}
	
	public void login() {
		if(lastControler == null)
			return;
		controler = ControlerHandler.getControler(lastControler);
		if(controler == null) {
			forceStop();
			return;
		}
		controler.setPlayer(player);
		if(controler.login())
			forceStop();
		else
			inited = true;
	}
	
	public void logout() {
		if(controler == null)
			return;
		if(controler.logout())
			forceStop();
	}
	
	
	public boolean canAttack(Entity entity) {
		if(controler == null || !inited)
			return true;
		return controler.canAttack(entity);
	}
	
	public boolean canHit(Entity entity) {
		if(controler == null || !inited)
			return true;
		return controler.canHit(entity);
	}
	
	public void moved() {
		if(controler == null || !inited)
			return;
		controler.moved();
	}
	
	public void sendInterfaces() {
		if(controler == null || !inited)
			return;
		controler.sendInterfaces();
	}
	
	public void process() {
		if(controler == null || !inited)
			return;
		controler.process();
	}
	
	public boolean sendDeath() {
		if(controler == null)
			return true;
		return controler.sendDeath();
	}
	
	public boolean useDialogueScript(Object key) {
		if(controler == null || !inited)
			return true;
		return controler.useDialogueScript(key);
	}
	
	
	public boolean processMagicTeleport(WorldTile toTile) {
		if(controler == null || !inited)
			return true;
		return controler.processMagicTeleport(toTile);
	}
	
	public boolean processItemTeleport(WorldTile toTile) {
		if(controler == null || !inited)
			return true;
		return controler.processItemTeleport(toTile);
	}
	
	public boolean processObjectClick1(WorldObject object) {
		if(controler == null || !inited)
			return true;
		return controler.processObjectClick1(object);
	}
	
	public boolean processObjectClick2(WorldObject object) {
		if(controler == null || !inited)
			return true;
		return controler.processObjectClick2(object);
	}
	
	public boolean processObjectClick3(WorldObject object) {
		if(controler == null || !inited)
			return true;
		return controler.processObjectClick3(object);
	}
	
	public void forceStop() {
		if(controler != null) {
			controler.forceClose();
			controler = null;
		}
		lastControlerArguments = null;
		lastControler = null;
		inited = false;
	}
	
	public void removeControlerWithoutCheck() {
		controler = null;
		lastControlerArguments = null;
		lastControler = null;
		inited = false;
	}
	
	public Object[] getLastControlerArguments() {
		return lastControlerArguments;
	}
	
	public void setLastControlerArguments(Object[] lastControlerArguments) {
		this.lastControlerArguments = lastControlerArguments;
	}
	
}
